from django.views.generic import View
from django.http import JsonResponse, FileResponse
from ..models.Literature import Literature
import traceback
import base64

class LiterDownloadShowFileView(View):
    def get(self, request, id):
        res = {"code": 400, "message": "", "data": None}
        try:
            liter = Literature.objects.get(uuid=id)
            pdf_path = liter.db_src
            
            with open(pdf_path, 'rb') as f:
                pdf_bytes = f.read()
            
            pdf_base64 = base64.b64encode(pdf_bytes).decode('utf-8')

            res["code"] = 200
            res["message"] = "文献源文件传输成功"
            res["data"] = pdf_base64
        except Exception as e:
            traceback.print_exc()
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)